package com.ruoyi.project.hissystem.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hissystem.domain.HisUser;
import com.ruoyi.project.hissystem.service.HisUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @author : 杨磊
 * @date : 14:46 2021/8/18
 */
@RestController
@RequestMapping("/hissystem/user")
public class HisUserController  extends BaseController {

    //声明业务层属性
    @Autowired
    private HisUserService hisUserService;

    /**
     * 1.获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('hissystem:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisUser hisUser)
    {
        startPage();
        List<HisUser> hisUsers = hisUserService.selectUserList(hisUser);
        return getDataTable(hisUsers);
    }


    /**
     * 3.新增用户
     */
    @PreAuthorize("@ss.hasPermi('hissystem:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HisUser hisUser)
    {
        hisUser.setCreateBy(SecurityUtils.getUsername());
        hisUser.setPassword(SecurityUtils.encryptPassword(hisUser.getPassword()));
        return toAjax(hisUserService.insertUser(hisUser));
    }

    /**
     * 4.根据用户编号获取详细信息(实现回显)
     */
    @PreAuthorize("@ss.hasPermi('hissystem:user:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable Long userId)
    {
        HisUser hisUser = hisUserService.selectUserById(userId);
        return AjaxResult.success(userId);

    }

    /**
     * 5.修改用户
     */
    @PreAuthorize("@ss.hasPermi('hissystem:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HisUser hisUser)
    {
        hisUser.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hisUserService.updateUser(hisUser));
    }

    /**
     * 6.删除用户
     */
    @PreAuthorize("@ss.hasPermi('hissystem:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(hisUserService.deleteUserByIds(userIds));
    }

    /**
     * 7.导出用户信息
     * @param hisUser
     * @return
     */
     @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hissystem:user:export')")
    @GetMapping("/export")
    public AjaxResult export(HisUser hisUser)
    {
        List<HisUser> list = hisUserService.selectUserList(hisUser);
        ExcelUtil<HisUser> util = new ExcelUtil<HisUser>(HisUser.class);
        return util.exportExcel(list, "用户数据");
    }
}
